<?php

namespace app\home\model\order;

use app\admin\model\channel\Channel;
use app\admin\model\merchant\MerchantModel;
use think\db\Query;
use think\facade\Session;
use think\Model;

class Order extends Model
{


    protected $pk = 'id';

    protected $json=['third_ext_json','pay_ext_json'];
    protected $jsonAssoc=true;

    //订单状态订单状态 0待支付 1已支付 2处理中 4交易失败,当值为2时，需间隔3秒重复查询状态


    public function getStatusAttr($value)
    {

        $type=[0=>'待支付','1'=>'已支付','2'=>'处理中','4'=>'交易失败'];
        return $type[$value];
    }

    public function GetChannelInfo(){

        return $this->hasOne(Channel::class,'id','channel_id')->field(['id','name as channel_code'])->bind(['channel_code']);

    }

    public function GetMerchantInfo(){

        return $this->hasOne(MerchantModel::class,'id','merchant_id')->field(['id','name as merchant_name'])->bind(['merchant_name']);

    }


    public static function list(){

            $params=input('','','trim');
            $where=[];
            isset($params['keyword']) ?  $where[]=['order_sn|out_trade_id','like','%'.$params['keyword'].'%']:false;
            isset($params['status']) ?  $where[]=['status','=',$params['status']]:false;
            isset($params['create_time']) ?  $where[]=['create_time','between',explode(',',$params['create_time'])]:false;
          $where[]=['merchant_id','=',\session('member_id')];
            $result=self::where($where)
                ->with(['GetChannelInfo','GetMerchantInfo'])
                ->page(input('page',1))->limit(input('limit',20))->order(['create_time'=>'desc'])->select();
            return [
                'code'=>200,
                'msg'=>'success',
                'total'=>self::where($where)->count(),
                'data'=>$result
            ];
    }
}